package com.huidatech.mpgdemo.crawler_unit;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huidatech.mpgdemo.entity.Price;
import com.huidatech.mpgdemo.service.PriceService;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: Wangzz
 * @Version: 1.0.0
 * @CreateDate: Created in 2021/10/24 23:23
 * @UpdateDate: [dateFormat:YYYY-MM-DD][modifier][description]
 */
@Service
public class CrawlerForPriceService {
    @Autowired
    private PriceService priceService;

    /**
     * 根据传入的站点源爬取网站的内容并存储或更新到数据库中
     * @param urlOriginalSeed 站点源
     * @throws Exception
     */
    public void executeCrawlerProgram(String urlOriginalSeed) throws Exception{
        // 获得URL种子
        List<String> urlSeeds = CrawlerForPriceUtils.getUrlSeeds(urlOriginalSeed);
        // 获得URL种子对应文档对象
        List<Document> urlDocuments = CrawlerForPriceUtils.getUrlDocuments(urlSeeds);
        // 结果集
        List<Price> priceList = new ArrayList<>();
        urlDocuments.forEach(document -> {
            CrawlerForPriceUtils.getResourceFromEachSource(document, priceList);
        });
        priceList.forEach(price -> {
            LambdaUpdateWrapper<Price> pluWrapper = new LambdaUpdateWrapper<>();
            pluWrapper.eq(Price::getProductName, price.getProductName())
                       .eq(Price::getMarketName, price.getMarketName());
            priceService.saveOrUpdate(price, pluWrapper);
        });
    }

}
